package com.project.capture5.app;

import com.project.capture5.bean.UserBehavior;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @author Shelly An
 * @create 2020/9/18 15:19
 */
public class PageViewByProcess {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //1. 从文件读取数据，转换成bean对象
        SingleOutputStreamOperator<UserBehavior> userBehaviorDS = env.readTextFile("Data/UserBehavior.csv")
                .map(new MapFunction<String, UserBehavior>() {
                    @Override
                    public UserBehavior map(String value) throws Exception {
                        String[] datas = value.split(",");

                        return new UserBehavior(Long.valueOf(datas[0]),
                                Long.valueOf(datas[1]),
                                Integer.valueOf(datas[2]),
                                datas[3],
                                Long.valueOf(datas[4]));
                    }
                });


        //2. 参考WordCount思路，实现pv的统计
        //2.1 过滤出pv行为
        SingleOutputStreamOperator<UserBehavior> userBehaviorFilter = userBehaviorDS.filter(
                (FilterFunction<UserBehavior>) value -> "pv".equals(value.getBehavior()));

        //2.2 按照统计的维度分组 ： pv行为
        KeyedStream<UserBehavior, String> userBehaviorKS = userBehaviorFilter.keyBy(UserBehavior::getBehavior);

        //2.3 求和 => 实现 计数的功能，没有count这种聚合算子
        //一般找不到合适的算子，一般调用底层的process，自己写
        SingleOutputStreamOperator<Long> resultDS = userBehaviorKS.process(new KeyedProcessFunction<String, UserBehavior, Long>() {
            //定义一个变量来统计条数
            private Long pvCount = 0L;

            /**
             * 来一条处理一条
             * @param value
             * @param ctx
             * @param out
             * @throws Exception
             */
            @Override
            public void processElement(UserBehavior value, Context ctx, Collector<Long> out) throws Exception {
                //来一条，count+1
                pvCount++;
                //统计器往下游发送统计结果
                out.collect(pvCount);
            }
        });

        resultDS.print("pv by process");

        env.execute();
    }
}
